package com.whfc.openapi.api.fim.controller;

import com.whfc.common.result.ListData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.fim.dto.FimAlgConfigDTO;
import com.whfc.fim.dto.FimAlgDeviceDTO;
import com.whfc.fim.dto.FimAlgTypeDTO;
import com.whfc.fim.param.FimAlgResultParam;
import com.whfc.fuum.dto.open.OpenApiClientDTO;
import com.whfc.openapi.api.fim.service.FimAlgResultService;
import com.whfc.openapi.constant.OpenApiConst;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @author: xugcheng
 * @version: 1.0
 * @date: 2019/10/23 17:00
 */
@RestController
@RequestMapping("open/api/v1/fim")
public class FimAlgResultController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private FimAlgResultService fimAlgResultService;

    @PostMapping("/alg/result")
    public Result uploadAlgResult(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO clientDTO,
                                  @RequestBody @Validated FimAlgResultParam fimAlgResultParam) {
        logger.info("视频算法检测结果上传,client:{},fimAlgResultParam:{}", clientDTO, fimAlgResultParam);
        fimAlgResultParam.setDeptId(clientDTO.getDeptId());
        fimAlgResultService.algResultUpload(fimAlgResultParam);
        return ResultUtil.success();
    }

    @PostMapping("/alg/result/upload")
    public Result algResultUpload(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO clientDTO,
                                  @RequestParam("algDeviceId") Integer algDeviceId,
                                  @RequestParam("deviceName") String deviceName,
                                  @RequestParam("type") Integer type,
                                  @RequestParam("typeName") String typeName,
                                  @RequestParam("time") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date time,
                                  @RequestParam(value = "img", required = false) MultipartFile img) {
        String imgName = img == null ? null : img.getOriginalFilename();
        logger.info("视频算法检测结果上传,client:{},algDeviceId:{},deviceName:{},type:{},typeName:{},time:{},img:{}",
                clientDTO, algDeviceId, deviceName, type, typeName, time, imgName);
        FimAlgResultParam fimAlgResultParam = new FimAlgResultParam();
        fimAlgResultParam.setDeptId(clientDTO.getDeptId());
        fimAlgResultParam.setDeviceId(algDeviceId);
        fimAlgResultParam.setDeviceName(deviceName);
        fimAlgResultParam.setAlgCheckType(type);
        fimAlgResultParam.setAlgCheckTypeName(typeName);
        fimAlgResultParam.setTime(time);
        fimAlgResultService.algResultUpload(fimAlgResultParam, img);
        return ResultUtil.success();
    }


    @GetMapping("/alg/config")
    public Result algConfig(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO clientDTO) {
        logger.info("获取算法配置，client:{}", clientDTO);
        List<FimAlgConfigDTO> list = fimAlgResultService.getAlgConfig(clientDTO.getDeptId());
        return ResultUtil.success(new ListData<>(list));
    }


    @GetMapping("/alg/device/list")
    public Result algDeviceList(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO clientDTO) {
        logger.info("获取摄像头信息列表，client:{}", clientDTO);
        List<FimAlgDeviceDTO> list = fimAlgResultService.deviceConfigList(clientDTO.getDeptId());
        return ResultUtil.success(new ListData<>(list));
    }


    @GetMapping("/alg/type/list")
    public Result algTypeList(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO clientDTO) {
        logger.info("获取算法类型列表，client:{}", clientDTO);
        List<FimAlgTypeDTO> algTypeList = fimAlgResultService.algTypeList(clientDTO.getDeptId());
        return ResultUtil.success(new ListData<>(algTypeList));
    }

}
